#!/bin/sh

# This is the oxdvi wrapper script, borrowed from xdvi.sh

# This script sets some environment variables to make sure that oxdvi's
# resource file in $TEXMF/xdvi is read by oxdvi.bin.
# Special care was taken to make this work for old R3, too. Therefore,
# we need to modify XAPPLRESDIR. If you are running R4 or later, you
# can set XUSERFILESEARCHPATH for user specific application default
# files. You cannot use XAPPLRESDIR for user specific application default
# files.

test -f /bin/sh5 && test -z "$RUNNING_SH5" \
  && test x`(uname -s) 2>&1` = xULTRIX \
  && { RUNNING_SH5=true; export RUNNING_SH5; exec /bin/sh5 $0 ${1+"$@"}; }
RUNNING_SH5=

test -f /bin/bsh && test -z "$RUNNING_BSH" \
  && test x`(uname -s) 2>&1` = xAIX \
  && { RUNNING_BSH=true; export RUNNING_BSH; exec /bin/bsh $0 ${1+"$@"}; }
RUNNING_BSH=

# -help and -version only work if they are the only options. Therefore,
# we cannot give the "-name xdvi" parameter in that case.
case "$#:$1" in
  1:-help|1:-version)
    NAMEOPT=;;
  *)
    NAMEOPT='-name xdvi';;
esac

# The main system texmf tree:
: ${TEXMFMAIN=`kpsewhich -expand-var='$TEXMFMAIN'`}
if test -z "$TEXMFMAIN"; then
  echo "$0: No \$TEXMFMAIN; set the environment variable or in texmf.cnf." >&2
  exit 1
fi

# The texmf tree where we put generated files into. Defaults to system tree:
: ${VARTEXMF=`kpsewhich -expand-var='$VARTEXMF'`}
test -n "$VARTEXMF" || VARTEXMF=$TEXMFMAIN

# The directory where we search for private scripts and config files.
# Defaults to $VARTEXMF/web2c.
: ${TEXMFCNF_DIR=`kpsewhich -expand-var='$TEXMFCNF_DIR'`}
test -n "$TEXMFCNF_DIR" || TEXMFCNF_DIR=$VARTEXMF/web2c

if test ! -f $TEXMFCNF_DIR/XDvi; then
  test -d "$TEXMFCNF_DIR" ||
    $TEXMFMAIN/web2c/MakeTeXmkdir $TEXMFCNF_DIR
  cp $TEXMFMAIN/web2c/XDvi $TEXMFCNF_DIR/XDvi
fi

xdviappdir="${TEXMFCNF_DIR}/"
xdviapppath="${xdviappdir}%N"

# For R3, we have to set XAPPLRESDIR.
XAPPLRESDIR="$xdviappdir"; export XAPPLRESDIR

# For R4 or later, we have to set XFILESEARCHPATH, since XAPPLRESDIR might
# be ignored (if XUSERFILESEARCHPATH is set)
XFILESEARCHPATH="$xdviapppath:${XFILESEARCHPATH-%D}"; export XFILESEARCHPATH

exec oxdvi.bin $NAMEOPT ${1+"$@"}
